from typing import NamedTuple
import re

import message as message_module
import video as video_module
import input_data as input_data_module


class InteractionTerminal:
    """ Взаимодействуем с пользователем через терминал """

    def __init__(self):
        self.message_object = message_module.Message()
        self.message_terminal_object = message_module.MessageTerminal()
        self.video_object = video_module.Video()
        self.input_data_object = input_data_module.InputDataFromUserFromTerminal()


    def start(self, input_arguments: NamedTuple) -> None:
        """ Начинаем взаимодействовать с пользователем """

        self.message_object.show_welcome_message(
            link_to_video=input_arguments.link_to_video
        )
        yt_object = self.video_object.get_video_object(
            link_to_video=input_arguments.link_to_video
        )
        basic_information_of_video = self.video_object.get_basic_information_of_video(
            yt_object,
            format=True
        )
        self.message_terminal_object.show_basic_information_of_video(
            basic_information_of_video
        )
        list_of_possible_streams = self.video_object.get_list_of_possible_streams(
            yt_object,
            input_arguments.is_all_downloads,
            convert_to_dict=True,
        )
        self.message_terminal_object.show_video_download_options(
            list_of_possible_streams
        )
        self.message_terminal_object.show_possible_actions()
        selected_response = self.input_data_object.get_user_selected_response()
        self.perform_selected_action(
            list_of_possible_streams,
            selected_response
        )


    def perform_selected_action(self, list_of_possible_streams, response):
        """ Выполняем выбранное действие пользователем """

        if re.findall('\d', response):
            response = int(response)
            content = list_of_possible_streams[response]
            self.video_object.download_selected_content(content)
        elif response == 'quit':
            exit(1)
        else:
            self.message_terminal_object.report_invalid_response()
            selected_response = self.input_data_object.get_user_selected_response()
            self.perform_selected_action(
                list_of_possible_streams,
                selected_response
            )

